<?php
App::uses('AppController', 'Controller');
/**
 * courses Controller
 *
 * @property course $course
 */
class CoursesController extends AppController {
	public $components = array('RequestHandler', 'AjaxMultiUpload.Upload');
	/**
	 * index method
	 *
	 * @return void
	 */
	public function index() {
		
		$this->set('courses', $this->paginate());

	}

	public function changeCourse(){
		$this->loadModel('Discipline');
		$this->loadModel('CoursesDiscipline');
		$disciplines = $this->Discipline->find('list', array('fields' => array('Discipline.id', 'Discipline.course')));
		$coursesL = $this->Course->find('list', array('fields' => array('Course.acronym', 'Course.id')));
		foreach ($disciplines as $key => $value) {
			$data = array();
			$courses = explode("+", $value);
			foreach ($courses as $course) {
					$data[] = array('CoursesDiscipline' => array('discipline_id' => $key, 'course_id' => $coursesL[$course]));
			}
			$this->CoursesDiscipline->saveMany($data);
		}
	}

	/**
	 * view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function view($id = null) {
		$this->Course->id = $id;
		if (!$this->Course->exists()) {
			throw new NotFoundException(__('Invalid course'));
		}
		$this->set('course', $this->Course->read(null, $id));
	}

	/**
	 * add method
	 *
	 * @return void
	 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Course->create();
			$this->request->data['Course']['_user'] = AuthComponent::user('id');
			if ($this->Course->save($this->request->data)) {
				$this->Session->setFlash(__('The course has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The course could not be saved. Please, try again.'));
			}
		}
	}

	public function beforeFilter(){
		$this->isAuthorized(array('admin', 'coordinator'));
	}

	/**
	 * edit method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function edit($id = null) {
		$this->Course->id = $id;
		if (!$this->Course->exists()) {
			throw new NotFoundException(__('Invalid course'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['Course']['_user'] = AuthComponent::user('id');
			if ($this->Course->save($this->request->data)) {
				$this->Session->setFlash(__('The course has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The course could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Course->read(null, $id);
		}
	}

	/**
	 * delete method
	 *
	 * @throws MethodNotAllowedException
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function delete($id = null) {
		
			if (!$this->request->is('post')) {
				throw new MethodNotAllowedException();
			}
			$this->Course->id = $id;
			if (!$this->Course->exists()) {
				throw new NotFoundException(__('Invalid course'));
			}
			if ($this->Course->delete()) {
				$this->Session->setFlash(__('course deleted'));
				$this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('course was not deleted'));
			$this->redirect(array('action' => 'index'));
		
	}

}
